// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0

export global Glob {
    in-out property <[string]> model: ["Hello", "World"];
    in-out property <bool> condition: true;
    // Return true if issue #3464 is NOT fixed
    callback clicked(string) -> bool;
    clicked => {
        model = ["oops"];
        return true; // 3464 is not fixed in the interpreter
    }
    in-out property <string> value;
    in-out property <string> value_after: "XXXXX";
}

component Button {
    callback clicked();
    in property <string> text;
    in property enabled <=> fs.enabled;

    HorizontalLayout {
        fs := FocusScope {
            width: 1px;
            height: 3%;
            key-pressed(k) => {
                if k.text == " " {
                    clicked();
                    return accept;
                }
                return reject;
            }
        }
        Rectangle {}
    }
    Rectangle {
        background: yellow;
        Text { text: text; }
        TouchArea {
            clicked => {clicked();}
            enabled: root.enabled;
        }
    }
}


export component TestCase inherits Window {
    width: 300px;
    height: 300px;
    in property <bool> some-variable: true;
    VerticalLayout {
        for string in Glob.model: Rectangle {
            if Glob.condition: Button {
                text: string;
                enabled: some-variable;
                width: 80%;
                height: 80%;
                clicked => {
                    Glob.value = string;
                    if Glob.clicked(string) {
                        // FIXME! issue #3464 not fixed
                        return;
                    }
                    Glob.value_after = string;
                }
            }
        }
        Rectangle {}
    }
}

/*
```rust
let instance = TestCase::new().unwrap();
let model = std::rc::Rc::new(slint::VecModel::<slint::SharedString>::from(
    vec!["hello".into(), "world".into()]));
instance.global::<Glob<'_>>().set_model(model.clone().into());
instance.global::<Glob<'_>>().on_clicked(move |val|{
    assert_eq!(val, "world");
    model.remove(1);
    false
});
slint_testing::send_mouse_click(&instance, 150., 150.);
assert_eq!(instance.global::<Glob<'_>>().get_value(), "world");
assert_eq!(instance.global::<Glob<'_>>().get_value_after(), "");
```

// same thing but with the keyboard
```rust
let instance = TestCase::new().unwrap();
let model = std::rc::Rc::new(slint::VecModel::<slint::SharedString>::from(
    vec!["hello".into(), "world".into()]));
instance.global::<Glob<'_>>().set_model(model.clone().into());
instance.global::<Glob<'_>>().on_clicked(move |val|{
    assert_eq!(val, "world");
    model.remove(1);
    false
});
slint_testing::send_keyboard_string_sequence(&instance, "\t");
slint_testing::send_keyboard_string_sequence(&instance, "\t");
slint_testing::send_keyboard_string_sequence(&instance, " ");
slint_testing::send_keyboard_string_sequence(&instance, " ");
slint_testing::send_keyboard_string_sequence(&instance, "\t");
assert_eq!(instance.global::<Glob<'_>>().get_value(), "world");
assert_eq!(instance.global::<Glob<'_>>().get_value_after(), "");
```

```cpp
auto handle = TestCase::create();
TestCase &instance = *handle;

std::vector<slint::SharedString> array;
array.push_back("hello");
array.push_back("world");
auto model = std::make_shared<slint::VectorModel<slint::SharedString>>(std::move(array));
instance.global<Glob>().set_model(model);
instance.global<Glob>().on_clicked([=](slint::SharedString val){
    assert_eq(val, "world");
    model->erase(1);
    return false;
});
slint_testing::send_mouse_click(&instance, 150., 150.);
assert_eq(instance.global<Glob>().get_value(), "world");
```

```cpp
auto handle = TestCase::create();
TestCase &instance = *handle;

std::vector<slint::SharedString> array;
array.push_back("hello");
array.push_back("world");
auto model = std::make_shared<slint::VectorModel<slint::SharedString>>(std::move(array));
instance.global<Glob>().set_model(model);
instance.global<Glob>().on_clicked([=](slint::SharedString val){
    assert_eq(val, "world");
    model->erase(1);
    return false;
});
slint_testing::send_keyboard_string_sequence(&instance, "\t");
slint_testing::send_keyboard_string_sequence(&instance, "\t");
slint_testing::send_keyboard_string_sequence(&instance, " ");
slint_testing::send_keyboard_string_sequence(&instance, " ");
slint_testing::send_keyboard_string_sequence(&instance, "\t");
assert_eq(instance.global<Glob>().get_value(), "world");
```


```js
var instance = new slint.TestCase({});
let model = new slintlib.ArrayModel(["hello", "world"]);
instance.Glob.model = model;
instance.Glob.clicked = (val) => {
    assert.equal(val, "world");
    model.remove(1, 1);
    return true; // issue 3464 not fixed in the interpreter
};
slintlib.private_api.send_mouse_click(instance, 150., 150.);
assert.equal(instance.Glob.value, "world");
```

```js
var instance = new slint.TestCase({});
let model = new slintlib.ArrayModel(["hello", "world"]);
instance.Glob.model = model;
instance.Glob.clicked = (val) => {
    assert.equal(val, "world");
    model.remove(1, 1);
    return true; // issue 3464 not fixed in the interpreter
};
slintlib.private_api.send_keyboard_string_sequence(instance, "\t");
slintlib.private_api.send_keyboard_string_sequence(instance, "\t");
slintlib.private_api.send_keyboard_string_sequence(instance, " ");
slintlib.private_api.send_keyboard_string_sequence(instance, " ");
slintlib.private_api.send_keyboard_string_sequence(instance, "\t");
assert.equal(instance.Glob.value, "world");
```


*/
